package com.example.bysj.Controller;
//201902104061徐中堂
import com.example.bysj.Service.StudentService;
import com.example.bysj.domain.Student;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//声明此类是一个Controller对象，返回指定文件形式
@Controller
//响应方法都是以该地址作为父路径
@RequestMapping("/studentInfoSys")
public class StudentController {
    private final static Logger logger = LoggerFactory.getLogger(StudentController.class);
    @Autowired
    StudentService studentService;

    /**
     * 添加学生
     * @param student
     * @return
     */
    @PostMapping("/student")
    @RequiresPermissions("student:post")
    @ResponseBody
    public String addStudent(@RequestBody Student student){
        logger.info(student.toString());
        return studentService.addStudent(student);
    }

    /**
     *批量添加学生
     * @param students 学生集合
     * @return
     */
    @PostMapping("/students")
    @RequiresPermissions("student:post")
    @ResponseBody
    public String addStudents(@RequestBody List<Student> students){
        logger.debug(students.toString());
        return studentService.addStudents(students);
    }

    /**
     * 查询所有学生
     * @return
     */
    @GetMapping("/student")
    @RequiresPermissions("student:get")
    @ResponseBody
    public List<Student> getAll(){
        return studentService.getAll();
    }

    /**
     * /student/1
     * 根据编号查询学生
     * @param id 编号
     * @return
     */
    @GetMapping("/student/{id}")
    @RequiresPermissions("student:get")
    @ResponseBody
    public Student getOneByNo(@PathVariable("id") int id){
        Student student = studentService.getOneById(id);
        return student;
    }

    /**
     * /student/1
     * 根据编号no，更新学生信息
     * @param student 更新后学生的信息
     * @return
     */
    @PutMapping("/student/{id}")
    @RequiresPermissions("student:put")
    @ResponseBody
    public String updateStudent(@PathVariable("id") int id,@RequestBody Student student){
        return studentService.updateStudent(id,student);
    }

    /**
     * 根据编号删除学生
     * @param id 编号
     * @return
     */
    @DeleteMapping("/student/{id}")
    @RequiresPermissions("student:delete")
    @ResponseBody
    public String deleteStudent(@PathVariable("id") int id){
        return studentService.deleteStudent(id);
    }
}
